﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TYAJZWeb.Models;
using TYAJZWeb.Data;
using TYAJZWeb.Utils;
using System.Text;

namespace TYAJZWeb.Controllers
{
    public class UserController : BaseController
    {
        [CookiesAuthorize]
        public ActionResult List(int page)
        {
            var r = Context.Users.Where(t => t.IsDeleted == false)
                .OrderByDescending(t => t.Id)
                .Select(t => (UserListModel)t);
            int totalCount = r.Count();
            r = r.Skip((page - 1) * PAGESIZE).Take(PAGESIZE);
            return View(new PageList<UserListModel>(r, page, PAGESIZE, totalCount));
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.UserTypes = new List<SelectListItem>
            {
                new SelectListItem(){ Value=UserTypes.Admin.ToString(), Text="管理员"},
                new SelectListItem(){ Value=UserTypes.Editor.ToString(), Text="录入员"},
                new SelectListItem(){ Value=UserTypes.Operator.ToString(), Text="操作员"},
                new SelectListItem(){ Value=UserTypes.Viewer.ToString(), Text="查看组"},
            };
            return View("Edit");
        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult Add(UserEditModel model)
        {
            if (ModelState.IsValid)
            {
                if (Context.Users.SingleOrDefault(t => t.Name == model.UserName) == null)
                {
                    User c = (User)model;
                    c.PassWord = "123456";
                    c.CreateDT = DateTime.Now;
                    c.ModifiedDT = DateTime.Now;
                    c.IsDeleted = false;
                    Context.Users.InsertOnSubmit(c);
                    Context.SubmitChanges();
                    TempData.Add("Success", "添加成功");
                    return RedirectToAction("List");
                }
                TempData.Add("Error", "该用户已存在");
            }

            ViewBag.UserTypes = new List<SelectListItem>
            {
                new SelectListItem(){ Value=UserTypes.Admin.ToString(), Text="管理员"},
                new SelectListItem(){ Value=UserTypes.Editor.ToString(), Text="录入员"},
                new SelectListItem(){ Value=UserTypes.Operator.ToString(), Text="操作员"},
                new SelectListItem(){ Value=UserTypes.Viewer.ToString(), Text="查看组"},
            };
            return View("Edit", model);
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Edit(int id)
        {
            ViewBag.UserTypes = new List<SelectListItem>
            {
               new SelectListItem(){ Value=UserTypes.Admin.ToString(), Text="管理员"},
                new SelectListItem(){ Value=UserTypes.Editor.ToString(), Text="录入员"},
                new SelectListItem(){ Value=UserTypes.Operator.ToString(), Text="操作员"},
                new SelectListItem(){ Value=UserTypes.Viewer.ToString(), Text="查看组"},
            };

            return View("Edit", (UserEditModel)Context.Users.Single(t => t.Id == id));

        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult Edit(UserEditModel model)
        {
            if (ModelState.IsValid)
            {
                var c = Context.Users.Single(t => t.Id == model.Id);
                c.RealName = model.RealName;
                c.UserType = model.UserType;
                Context.SubmitChanges();
                TempData.Add("Success", "修改成功");
                return RedirectToAction("List");
            }

            ViewBag.UserTypes = new List<SelectListItem>
            {
               new SelectListItem(){ Value=UserTypes.Admin.ToString(), Text="管理员"},
                new SelectListItem(){ Value=UserTypes.Editor.ToString(), Text="录入员"},
                new SelectListItem(){ Value=UserTypes.Operator.ToString(), Text="操作员"},
                new SelectListItem(){ Value=UserTypes.Viewer.ToString(), Text="查看组"},
            };
            return View("Edit", model);
        }

        [CookiesAuthorize]
        public ActionResult Delete(int id)
        {
            var c = Context.Users.Single(t => t.Id == id);
            c.IsDeleted = true;
            Context.SubmitChanges();

            TempData.Add("Success", "删除成功");

            return RedirectToAction("List");

        }

        [CookiesAuthorize]
        public ActionResult Detail(int id)
        {
            var u = Context.Users.SingleOrDefault(t => t.Id == id);
            return View((UserDetailModel)u);
        }

        [CookiesAuthorize]
        [HttpGet]
        public ActionResult ChangePassWord(int id)
        {
            if (CurrentUserId == id)
            {
                var u = Context.Users.SingleOrDefault(t => t.Id == id);
                return View((UserChangePasswordModel)u);
            }
            else
            {
                return View("Error");
            }
        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult ChangePassWord(UserChangePasswordModel model)
        {

            if (CurrentUserId == model.Id)
            {
                var u = Context.Users.SingleOrDefault(t => t.Id == model.Id);
                if (!String.IsNullOrEmpty(model.Password) && model.Password == model.RetypePassword)
                {
                    u.PassWord = model.Password;
                    Context.SubmitChanges();
                    TempData.Add("Success", "密码修改成功");
                    return RedirectToAction("ChangePassWord", new { id = model.Id });
                }
                else
                {
                    if (String.IsNullOrEmpty(model.Password))
                        TempData.Add("Error", "密码不能为空");
                    else
                        TempData.Add("Error", "两次密码不一致");
                }
                return View((UserChangePasswordModel)u);
            }
            else
            {
                return View("Error");
            }
        }
    }
}
